﻿using AutoMapper;
using DocumentFormat.OpenXml.EMMA;
using MediatR;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;

namespace MyMES.Writer.API.Application.CommendHandlers.BOMs
{
    public class AddBomHandler : IRequestHandler<AddBomCommand, APIResult<BOM>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<BOM> repository;

        /// <summary>
        /// 添加BOM
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="repository"></param>
        public AddBomHandler(IMapper mapper,IBaseRepository<BOM> repository)
        {
            this.mapper = mapper;
            this.repository = repository;
        }

        public async Task<APIResult<BOM>> Handle(AddBomCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<BOM> result = new APIResult<BOM>();
                result.Code = APIEnum.添加成功;
                result.Msg = "添加成功";
                var aa = mapper.Map<BOM>(request);
                var sql = "insert into  BOM (BomId,Itemnumber,BOMunit,BOMmaterial,ExpectedUsage,BomType,IsDlete) values (@BomId,@Itemnumber,@BOMunit,@BOMmaterial,@ExpectedUsage,@BomType,@IsDlete)";
                await repository.Insert(aa, sql);
                return result;
            }
            catch (Exception)
            {

                throw;
            }
         
        }
    }
}
